import Vue from 'vue'
import Router from 'vue-router'
import hell from "@/components/hell"//@代表src
import Login from "@/components/Login"
import home from  "@/components/home"
import welcome from "@/components/welcome"
import users from  "@/components/user/User"
import rights from "@/components/power/rights"
import roles from "@/components/power/roles"

Vue.use(Router)

const router=new Router({
  routes: [
    {
      path: '/aa',
      name: 'hell',
      component: hell
    },
    {
      path: "/login",
      name: "login",
      component: Login
    },
    {
      path: "/",
      //name:"/login1",
      redirect:"/login"
    },
    {
      path: "/home",
      name: "home",
      component: home,
      redirect:"welcome",
      children: [
        {path:"/welcome" , name:"welcome" ,component :welcome},
        {path:"/users",component:users},
        {path:"/rights",component:rights},
        {path:"/roles",component:roles}
      ]
    }
  ]
})
//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to代表访问的路径
  //from 代表从哪一个路径来
  //next 是函数，代表放行 next()放行，next("/XX")强制跳转

  //去login，直接放行
  if(to.path === "/login") return next()
  
  const token=window.sessionStorage.getItem("token")
  if(!token) return next("/login")//无token，去login
  next()//直接方行


})

export default router
